import 'dart:io';

import 'package:auto_route/annotations.dart';
import 'package:extended_image/extended_image.dart';
import 'package:flutter/material.dart';
import 'package:photo_view/photo_view.dart';
import 'package:photo_view/photo_view_gallery.dart';

@RoutePage()
class ReviewImagePage extends StatefulWidget {
  final String images;
  final int index;

  const ReviewImagePage({
    super.key,
    @pathParam required this.images,
    @pathParam required this.index,
  });

  @override
  State<ReviewImagePage> createState() => _ReviewImagePageState();
}

class _ReviewImagePageState extends State<ReviewImagePage> {
  late PageController pageController;

  @override
  void initState() {
    super.initState();
    pageController = PageController(initialPage: widget.index);
  }

  @override
  void dispose() {
    pageController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    List<String> imageList = widget.images.split(',');
    return Scaffold(
        appBar: AppBar(
            iconTheme: const IconThemeData(
              color: Colors.white, // 设置箭头颜色
            ),
            backgroundColor: Colors.black),
        backgroundColor: Colors.black,
        body: PhotoViewGallery.builder(
          scrollPhysics: const BouncingScrollPhysics(),
          builder: (BuildContext context, int index) {
            var itemImage = imageList[index];
            return PhotoViewGalleryPageOptions(
              imageProvider: ExtendedNetworkImageProvider(itemImage),
              initialScale: PhotoViewComputedScale.contained,
              // heroAttributes: PhotoViewHeroAttributes(tag: galleryItems[index].id),
            );
          },
          itemCount: imageList.length,
          loadingBuilder: (context, event) => Center(
            child: SizedBox(
              width: 20.0,
              height: 20.0,
              child: CircularProgressIndicator(
                value: event == null
                    ? 0
                    : event.cumulativeBytesLoaded / event.expectedTotalBytes!,
              ),
            ),
          ),
          pageController: pageController,
          // onPageChanged: onPageChanged,
        ));
  }
}
